<?php
	
	require_once(dirname(__FILE__)."/../core/safeParam.php");
	
	$cart_name   = "WCMP";
	$cart_object = null;

	class faucartelement{
  		
  		# Reserved variables
  		var $object_id;
  		var $name;
  		var $description;
  		var $price;
  		var $quantity;
  		var $item_id;
  		var $item_options;
  		var $discount;
  		var $tax;
  		
  		function faucartelement($name, $description, $price, $quantity, $item_id = NULL, $item_options = NULL, $discount = NULL, $file = NULL, $tax = 1){
    		$this->name         = $name;
    		$this->description  = $description;
    		$this->price        = $price;
    		$this->quantity     = $quantity;
    		$this->item_id      = $item_id;
    		$this->item_options = $item_options;
    		$this->discount     = $discount;
    		$this->tax          = $tax;
    		$this->file         = $file;
    		$this->object_id    = md5($name . $description . $price);
  		}  		
	}
	
	//CLASE AGREGADA
	class itemTemporal{
		
		var $object_id;
		var $name;
		var $description = NULL;
		var $price;
		var $quantity;
  		var $item_id; 
		
		function itemTemporal($itemId, $itemNombre, $itemPrecio, $itemCantidad){
			$this->name 		= $itemNombre;
			$this->price		= $itemPrecio;
			$this->quantity	= $itemCantidad;		
  			$this->item_id		= $itemId;
  			$this->object_id	= md5($name . $price);
  		}
	
	}		
	
	class faucart{
		
  		# Reserved variables
  		var $_items;
  		var $_current_item;
  		var $_total;
  		
  		function faucart(){
    		$this->_items        = array();
    		$this->_current_item = 0;
    		$this->_total        = 0;
  		}
  		
  		function isFound($item_number){
    		for ($i=0; $i< count($this->_items); $i++) {
      		if (isset($this->_items[$i])) {
        			if ($this->_items[$i]->object_id == $item_number->object_id)
          			return $i;
      		} 
    		}     		
    		return false;
  		}
  		
  		//FUNCION AGREGADA
  		function isAt($itemTemp){
  			for($i=0; $i< count($this->_items); $i++){
  				if(isset($this->_items[$i])){
  					if($this->_items[$i]->item_id == $itemTemp->item_id){
  						return $i;
  					}
  				}	
  			}
  			return -1;
  		}
  		
  		function atIndex($object_id){
    		for ($i = 0; $i < count($this->_items); $i++) {
      		if (isset($this->_items[$i])) {
        			if ($this->_items[$i]->object_id == $object_id)
          			return $i;
      		} 
    		} 
    		return false;
  		}
  		
  		function addItem($item){
    		$index = $this->isFound($item);
    		
    		if ($index !== false) {
      		if (isset($_GET['quantity'])) {
        			$quantity = $_GET['quantity'];
      		} else {
        			$quantity = "";
      		}      		
      		$this->updateQuantity($item, $quantity , $index);
    		} else {
      		$this->_items[] = $item;
    		}
  		}

		//FUNCION AGREGADA  		
  		function addElement($item){
  			$pos = $this->isAt($item);
  			
  			if($pos == -1){
  				$this->_items[] = $item;
  			} else {
  				$this->_items[$pos]->quantity += $item->quantity;
  			}  			
  		}
  		
  		function delItem($item_number){
    		$temp_items = array();
    		for ($i = 0; $i < count($this->_items); $i++) {
      		if (isset($this->_items[$i])) {
        			if ($this->_items[$i]->object_id == $item_number->object_id)
          			continue;
        			else
          			$temp_items[] = $this->_items[$i];
      		}
    		}
    		$this->_items = $temp_items;
    		unset($temp_items);
  		}
  		
  		//FUNCION AGREGADA
  		function delElement($itemId){
  			$items_temp = array();
  			
  			for($i=0; $i< count($this->_items); $i++){
				if(isset($this->_items[$i])){
					if($this->_items[$i]->item_id == $itemId){
						continue;
					} else {
						$items_temp[] = $this->_items[$i];
					}  			
  				}
  			}
  			$this->_items = $items_temp;
  			unset($items_temp);
  		}
  		
  		function delItemByObjectID($object_id){
    		$temp_items = array();
    		for ($i = 0; $i < count($this->_items); $i++) {
      		if (isset($this->_items[$i])) {
        			if ($this->_items[$i]->object_id == $object_id)
          			continue;
        			else
          			$temp_items[] = $this->_items[$i];
      		}
    		}
    		$this->_items = $temp_items;
    		unset($temp_items);
  		}
  		
  		function getItems(){
    		return $this->_items;
  		}
  		
  		function updateQuantity($item, $delta, $index = false){
    		if ($index == false) {
      		$index = $this->isFound($item);
    		}
    		$this->_items[$index]->quantity += $delta;
  		}
  		 		
  		function emptyCart(){
    		unset($this->_items);
    		$this->_items = array();
    		$this->_total = 0;
  		}
  		
  		function total(){
    		$this->_total = 0;
    		for ($i=0; $i< count($this->_items); $i++) {
      		if (isset($this->_items[$i])) {
        			$this->_total += number_format((double) ($this->_items[$i]->quantity * $this->_items[$i]->price), 2, '.', '');
      		}
    		}
  		}
	}
		
	function inSession(){
  		global $cart_name;
  		global $cart_object;
  		
  		if (isset($_SESSION[$cart_name])) {
    		$result = unserialize($_SESSION[$cart_name]);
    			if ($result) {
      			$cart_object = $result;
    			}
  		} 

  		if (!$cart_object) {
    		$cart_object = new faucart();
  		}
  }
	
	function toSession(){
  		global $cart_name;
  		global $cart_object;
  		
  		if (isset($_SESSION[$cart_name]))
    		unset($_SESSION[$cart_name]);
  			$_SESSION[$cart_name] = serialize($cart_object);
	}
	
	inSession();
?>
